package dir_sync;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Test {

    public static void main(String[] args) {
        test3();

    }

    public static void test1() {
        File srcBaseDir = new File("D:\\");
        File dstBaseDir = new File("E:\\backup");

        File testFile = new File("D:\\Test\\Log\\tst.log");
        checkDstFileDiff(testFile, srcBaseDir, dstBaseDir);

        File testDir = new File("D:\\Test\\Log\\hello");
        checkDstFileDiff(testDir, srcBaseDir, dstBaseDir);

        File testRoot = new File("D:\\");
        checkDstFileDiff(testRoot, srcBaseDir, dstBaseDir);
    }

    public static void test2() {
        String srcBasePath = "D:\\";
        String dstBasePath = "E:\\\\backup";
        checkDstFileDiff2("D:\\Test\\Log\\tst.log", srcBasePath, dstBasePath);
        checkDstFileDiff2("D:\\Test\\Log\\hello", srcBasePath, dstBasePath);
        checkDstFileDiff2("D:\\Test", srcBasePath, dstBasePath);
        checkDstFileDiff2("D:\\Test\\\\Log", srcBasePath, dstBasePath);
        checkDstFileDiff2("D:\\", srcBasePath, dstBasePath);
        checkDstFileDiff2("D:\\Test\\Log\\\u00A0tst.log", srcBasePath, dstBasePath);
    }

    public static void test3() {
        String srcBasePath = "D:\\";
        String dstBasePath = "E:\\\\backup";
        checkDstFileDiff3("D:\\Test\\Log\\tst.log", srcBasePath, dstBasePath);
        checkDstFileDiff3("D:\\Test\\Log\\hello", srcBasePath, dstBasePath);
        checkDstFileDiff3("D:\\Test", srcBasePath, dstBasePath);
        checkDstFileDiff3("D:\\Test\\\\Log", srcBasePath, dstBasePath);
        checkDstFileDiff3("D:\\", srcBasePath, dstBasePath);
        checkDstFileDiff3("D:\\Test\\Log\\\u00A0tst.log", srcBasePath, dstBasePath);
    }

    public static void checkDstFileDiff3(String srcPath, String srcBasePath, String dstBasePath) {
        // 定义源路径和目标路径
        Path srcBase = Paths.get(srcBasePath);
        Path dstBase = Paths.get(dstBasePath);
        Path originalPath = Paths.get(srcPath);

        // Path srcBase = Paths.get("D:", "Test");
        // Path dstBase = Paths.get("E:", "backup");
        // Path originalPath = Paths.get("D:", "Test", "Log", "tst.log");

        // 替换路径
        Path newPath = dstBase.resolve(srcBase.relativize(originalPath));

        // 输出结果
        System.out.println("原始路径: " + originalPath);
        System.out.println("替换后的路径: " + newPath);
        System.out.println("替换后的路径: " + newPath.toFile().getName().contains("\u00A0"));
    }

    public static void checkDstFileDiff2(String srcPath, String srcBasePath, String dstBasePath) {
        System.out.println("---------------");

        String rightPart = srcPath.substring(srcBasePath.length());
        System.out.println("rightPart: " + rightPart);

        String dstPath;
        if (rightPart.length() == 0) {
            dstPath = dstBasePath;
        } else {
            dstPath = dstBasePath + File.separator + rightPart;
            System.out.println("dstPath===: " + dstPath);

            File tmp = new File(dstPath);
            String filename = tmp.getName();
            System.out.println("---" + filename);
            filename = removeLeftChineseSpace(filename);
            System.out.println(filename);
        }
        System.out.println("dstPath: " + dstPath);
    }

    public static void checkDstFileDiff(File src, File srcBaseDir, File dstBaseDir) {
        System.out.println("---------------");
        String sp = src.getAbsolutePath();
        System.out.println("sp: " + sp);

        String base = srcBaseDir.getAbsolutePath();
        System.out.println("srcbase: " + base);

        // 如果路径结尾带有路径分隔符，则不加1
        String srcPath;
        if (base.endsWith(File.separator)) {
            srcPath = sp.substring(base.length());
        } else {
            srcPath = sp.substring(base.length() + 1);
        }
        System.out.println("srcPath: " + srcPath);

        // 有些文件前后存在特殊不可见字符，如果加上windows判断成不存在.
        String partFilePath;

        int index = srcPath.lastIndexOf(File.separator);
        if (index == -1 || srcPath.length() < index + 1) {
            partFilePath = srcPath;
        } else {
            String fileName = srcPath.substring(srcPath.lastIndexOf(File.separator) + 1);
            fileName = removeLeftChineseSpace(fileName);
            String prefix = srcPath.substring(0, srcPath.lastIndexOf(File.separator));

            partFilePath = prefix + File.separator + fileName;

        }
        System.out.println("partFilePath: " + partFilePath);

        String dstPath = dstBaseDir.getAbsolutePath() + File.separator + partFilePath;

        System.out.println("dstPath: " + dstPath);

    }

    // 去掉文件名称左侧的不可见字符，不可见字符不仅是空格
    private static String removeLeftChineseSpace(String fileName) {
        // 只去掉文件名左侧的不间断空格字符（UTF-8 编码为 CE A0）
        if (fileName.startsWith("\u00A0")) {
            return fileName.substring(1);
        }
        return fileName;
    }

}
